/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import Algorithm.*;

/**
 * 这个类用于描述棋盘方格
 * @author ErikLee
 * @version 1.0
 *
 */
public class BoardSquare extends Rectangle2D.Double {
    /**棋盘*/
    private BoardPanel _panel;
    /**格子的颜色和单击的颜色*/
    private Color _myColor;

    private Piece myPiece;

    /**
     * 构造方法，初始化方格的尺寸和颜色
     *
     * @param x 方格左上角横坐标
     * @param y 方格左上角纵坐标
     * @param aWidth 方格的尺寸
     * @param aPanel 棋盘面板
     */
    public BoardSquare(int x, int y, int aWidth, BoardPanel aPanel) {
        super(x, y, aWidth, aWidth);
        _panel = aPanel;
        _myColor = Color.white;
        //_clickColor = Color.red;

        myPiece = new Piece(x, y, aWidth, TicTacToe.HUMAN);
    }

    /**
     * 设置当前方格应呈现的颜色
     *
     * @param aColor 鼠标点击的方格应该呈现的颜色
     */
    public void setColor(Color aColor) {
        _myColor = aColor;
    }

    /**
     * 设置当前方格应呈现的颜色
     *
     * @return 鼠标点击的方格应该呈现的颜色
     */
    public Color getColor() {
        return _myColor;
    }

    /**
     * 画方格
     *
     * @param aPaintBrush
     */
    public void fill(Graphics2D aPaintBrush) {
        Color savedColor = aPaintBrush.getColor();
        aPaintBrush.fill(this);

        aPaintBrush.setColor(myPiece.getColor());
        if(myPiece.getVisible())
            myPiece.drawPiece(aPaintBrush);

        aPaintBrush.setColor(savedColor);
        
    }

    /**
     * 鼠标点击了这个方格，改变它的颜色
     * @param e
     */
    public void mouseClicked(MouseEvent e) {
//        if (_myColor.equals(Color.white)) {
//            this.setColor(_clickColor);
//        } else {
//            this.setColor(Color.white);
//        }        
        putPiece(TicTacToe.HUMAN);

        _panel.repaint();
    }

    public void putPiece(int side) {
        myPiece.setSide(side);
        myPiece.setVisible(true);
    }

    public void restPiece() {
        myPiece.setVisible(false);
    }

    public void setPieceSide(int side) {
        myPiece.setSide(side);
    }

    public void setPieceVisible(boolean visible) {
        myPiece.setVisible(visible);
    }
}